package com.jacen.fzxs.util;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 操作系统工具类
 * @author Administrator
 *
 */
public class OSUtils {
	
	
	private static final Logger logger = LoggerFactory.getLogger(OSUtils.class);

	/**
	 * 获取当前操作系统名称
	 * @return
	 */
	public static String getOsName(){
		return System.getProperty("os.name");
	}
	
	/**
	 * 获取删除命令
	 * @param path 文件路径
	 * @return
	 */
	public static String getDelCmd(String path){
		String delCmd = "";
		if(getOsName().toUpperCase().startsWith("WINDOWS"))
			delCmd = "cmd /c del " + path;
		else
			delCmd = "rm -rf " + path;
		return delCmd;
	}
	
	/**
	 * 获取备份mysql数据库命令
	 * @param path 备份文件路径
	 * @return
	 * @throws IOException 
	 */
	public static Process getMysqlDbBackupCmd(String path) throws IOException{
		String backupCmd = "";
		if(getOsName().toUpperCase().startsWith("WINDOWS")){
			backupCmd = "cmd /c mysqldump -u root -p123456 test > " + path;
			return Runtime.getRuntime().exec(backupCmd);
		}
		else{
			backupCmd = getMysqlPath() + File.separator + "mysqldump -u root -p123456 test > " + path;
			logger.info(backupCmd);
			return Runtime.getRuntime().exec(new String[] { "sh", "-c", backupCmd });
		}
	}
	
	public static String getMysqlPath(){
		String path = "";
		try{
			Map<String, String> m=System.getenv();
			String s2=(String) m.get("Path");//获取本计算机环境变量中PATH的内容
			String s1=s2.substring(0, s2.indexOf("MySQL"));//截取索引从‘0’到”MySQL“字符串
			String s3=s2.substring(s1.lastIndexOf(":")-1, s2.length());//获取S1字符串中最后一个”：“的索引，然后截取从":"-1 索引处到最后的字符串
			path = s3.substring(0,s3.indexOf("bin")+3);//截取字符串S3 从索引”0“ 到”bin“的字符串。获取完整的mysql安装路径
		}catch(Exception e){
			logger.info(e.getMessage());
		}
		return path;
	}
	
	 public static void main(String[] args) {
		System.out.println(getMysqlPath());
	}
}


